package org.cc.cloud.hystrix;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.discovery.provider.Serializer;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
@Component
//@DefaultProperties(defaultFallback = "helloerror")  这个注解是hystrix的配置全局回滚方法
public class HystrixService {
	@Autowired
	public RestTemplate getRestTemplate;
//	
	
	@HystrixCommand(fallbackMethod = "helloerror", groupKey = "MemberGroup", commandKey = "MemberCommandKey", 
			commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
	}, threadPoolProperties = {
			@HystrixProperty(name = "coreSize", value = "2")
	})
	public String hello(){

		//return "";
		return getRestTemplate.getForObject("http://first-provider/hellown",String.class);
	}
	
	//使用缓存
	@CacheResult
	@HystrixCommand(commandKey = "cacheKey")
	public String getCache(Integer id) {
		System.out.println("执行查询方法");
		return null;
	}
	//删除缓存
	@CacheRemove(commandKey = "cacheKey")
	@HystrixCommand
	public void removeCache(Integer id) {
		System.out.println("删除缓存方法");
	}
	 public String helloerror() {
	        return "服务器繁忙请稍后再试！";
	 }
}
